package com.ruoyi.framework.aspectj;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.DoTask;
import com.ruoyi.common.enums.DoTaskEnum;
import com.ruoyi.system.entity.SysActing;
import com.ruoyi.system.service.AgencyTaskService;
import com.ruoyi.system.service.SysActingService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Order
@Slf4j
public class DoTaskAspect {
    @Autowired
    private SysActingService sysActingService;
    @Autowired
    private AgencyTaskService agencyTaskService;

    @Pointcut("@annotation(com.ruoyi.common.annotation.DoTask)")
    public void saveDoTask() {
    }

    ;

    @Before(value = "saveDoTask() && @annotation(doTask)")
    public void saveMessage(JoinPoint joinPoint, DoTask doTask) {
        DoTaskEnum doTaskEnum = doTask.saveDoTak();
        log.info("切入 通知:{}", JSON.toJSON(doTaskEnum));

        //未知类型,不做处理
        if (doTaskEnum.equals(DoTaskEnum.UNKNOWN)) {
            return;
        }

        //插入通知
        save(doTaskEnum);
    }

    private void save(DoTaskEnum doTaskEnum) {
        SysActing sysActing = new SysActing();
        sysActing.setTitleName(doTaskEnum.getTitleName());
        sysActing.setTitleType(doTaskEnum.getTitleType());
        sysActing.setSourceId(doTaskEnum.getSourceId());
        sysActing.setState(doTaskEnum.getState());
        sysActing.setTitleInfo(doTaskEnum.getTitleInfo());
        sysActing.setOriginatorName(doTaskEnum.getOriginatorName());
        sysActing.setOriginatorId(doTaskEnum.getOriginatorId());
        sysActing.setExecutorName(doTaskEnum.getExecutorName());
        sysActing.setExecutorId(doTaskEnum.getExecutorId());
        sysActing.setCompleteTime(doTaskEnum.getCompleteTime());
        sysActingService.insert(sysActing);
    }

}
